import axios from 'axios'
import { Message, MessageBox } from 'element-ui'
import store from '../store'
import { getToken } from '@/utils/auth'

import md5 from 'js-md5'
// 创建axios实例
const service = axios.create({
  baseURL: process.env.BASE_API, // api 的 base_url
  timeout: 5000 // 请求超时时间
})

// request拦截器
service.interceptors.request.use(
  config => {

    config.data='aeyi15Vp7DbRxPn+2zjQN8qgtFL9qPhJWNK24WT8ugI='


      config.headers['secert']='RMYgvllYgwSfg2X7OKsnXjWZDGo/ldmhnFPSoQeRHxS8yd6Q/RIi6qTcOq6rx65oY70EALsinnSwhgq/oClXAvmVd4izOGqTzeszVO9iPUeri/NEjrOdELXe7KQPQ+BISMIFBuYY4X6x0VffKO6jwUUquYAzu3D9yDQlJQswNWk='
      config.headers['authorization'] = 'bearer ' +'eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MSwiZXhwIjoxNTc2NTY0NzYxLCJ1c2VyX25hbWUiOiJhIiwianRpIjoiZmNkMWU2YzItZDI1MC00YTdkLTkxMjUtNmEyZTc4OGU0YzY0IiwiY2xpZW50X2lkIjoiYWp4dCIsInNjb3BlIjpbImFsbCIsInJlYWQiLCJ3cml0ZSJdfQ.GczD3jCa2uCIPvknq8CQAvUPQainT4fqeoid4jm6goX90hBj2X7jV8LT3QKPuFgMVFV5SkqMQlSWIgPp7IG9S9Tx7thAH2Pt1R1GqoBV9BvRdtJ4R9Xu0dirqwarq0q2QsPu6-3abUb4yyPeqNNWhx3AXSflBIZX-471d5r7oO3GdOk25EDxAbdMpGpFS9A93via-wUnwr-KKX50ltfIzm7yzZ7ykk6pDV7NsLjJ2Gi-sXkcOX9cFIGpxwKGY_7YNIgd9Dt_7v0IfoX-7WPJ1Plt9UC_dxaFpOjjW_i5h9jMuUF4FB-YtwXQKLzjrjwZaK_DDQrH6sgHl5AYWGdnTw' // 让每个请求携带自定义token 请根据实际情况自行修改
      config.headers['sign']='DE548E35B178880CB882F619C3C8CE5E'

    return config
  },
  error => {
    // Do something with request error
    console.log(error) // for debug
    Promise.reject(error)
  }
)

// response 拦截器
service.interceptors.response.use(
  response => {
    /**
     * code为非20000是抛错 可结合自己业务进行修改
     */
    const res = response

    if (res.status !== 200) {
      Message({
        message: res.message,
        type: 'error',
        duration: 5 * 1000
      })

      if (res.code === 400 || res.code === 401 || res.code === 402) {
        MessageBox.confirm('你已被登出，可以取消继续留在该页面，或者重新登录', '确定登出',
          { confirmButtonText: '重新登录',
            cancelButtonText: '取消',
            type: 'warning'
          }
        ).then(() => {
          //store.dispatch('FedLogOut').then(() => {
          location.reload() // 为了重新实例化vue-router对象 避免bug
          //})
        })
      }
      return Promise.reject('error')
    } else {
      const result=res.data
      if(result.code=='0000'){
        return response.data
      }else if (result.code == '400' || result.code == '401' || result.code == '402') {
        MessageBox.confirm('你已被登出，可以取消继续留在该页面，或者重新登录', '确定登出',
          { confirmButtonText: '重新登录',
            cancelButtonText: '取消',
            type: 'warning'
          }
        ).then(() => {
          //store.dispatch('FedLogOut').then(() => {
          location.reload() // 为了重新实例化vue-router对象 避免bug
          router.replace({ name: 'login' })
          // })
        })
        return Promise.reject('error')
      }else{
        Message({
          message: result.msg,
          type: 'warning',
          duration: 1500
        })
        return response.data
      }
    }
  },
  error => {
    console.log('err' + error) // for debug
    const errorResponse=error.response
    if (errorResponse.status === 401 || errorResponse.status === 402) {
      MessageBox.confirm('你已被登出，可以取消继续留在该页面，或者重新登录', '确定登出',
        {
          confirmButtonText: '重新登录',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).then(() => {
        //store.dispatch('FedLogOut').then(() => {
        location.reload() // 为了重新实例化vue-router对象 避免bug
        router.replace({ name: 'login' })
        //})
      })
      return Promise.reject('error')
    }else if(errorResponse.status===403){
      Message({
        message: "无法访问",
        type: 'error',
        duration: 5 * 1000
      })
      return Promise.reject(error)
    }
    Message({
      message: error.message,
      type: 'error',
      duration: 5 * 1000
    })
    return Promise.reject(error)
  }
)

export default service
